package com.esri.core.geometry;

import com.esri.core.geometry.Geometry;
import com.github.mikephil.charting.utils.Utils;
import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.io.Serializable;

/* loaded from: classes.dex */
final class GeometrySerializer implements Serializable {
    private static final long serialVersionUID = 1;
    BaseGeometryData a;

    /* loaded from: classes.dex */
    public static class BaseGeometryData implements Serializable {
        Geometry.Type a;
        byte[] b = null;

        BaseGeometryData() {
        }
    }

    /* loaded from: classes.dex */
    public static class MultiVertexData extends BaseGeometryData {
        int d = 0;
        double e = Utils.DOUBLE_EPSILON;

        MultiVertexData() {
        }
    }

    /* loaded from: classes.dex */
    public static class MultiPathData extends MultiVertexData {
        boolean[] c = null;

        MultiPathData() {
        }
    }

    Object readResolve() throws ObjectStreamException {
        try {
            Geometry geometryFromEsriShape = GeometryEngine.geometryFromEsriShape(this.a.b, this.a.a);
            if (Geometry.isMultiVertex(geometryFromEsriShape.getType().value())) {
                MultiVertexData multiVertexData = (MultiVertexData) this.a;
                MultiVertexGeometryImpl multiVertexGeometryImpl = (MultiVertexGeometryImpl) geometryFromEsriShape._getImpl();
                if (!geometryFromEsriShape.isEmpty() && Geometry.isMultiPath(geometryFromEsriShape.getType().value())) {
                    MultiPathData multiPathData = (MultiPathData) this.a;
                    MultiPathImpl multiPathImpl = (MultiPathImpl) geometryFromEsriShape._getImpl();
                    AttributeStreamOfInt8 pathFlagsStreamRef = multiPathImpl.getPathFlagsStreamRef();
                    int pathCount = multiPathImpl.getPathCount();
                    for (int i = 0; i < pathCount; i++) {
                        if (multiPathData.c[i]) {
                            pathFlagsStreamRef.setBits(i, (byte) 4);
                        }
                    }
                }
                multiVertexGeometryImpl.a(multiVertexData.d, multiVertexData.e);
            }
            return geometryFromEsriShape;
        } catch (Exception unused) {
            throw new InvalidObjectException("Cannot read geometry from stream");
        }
    }

    public void setGeometryByValue(Geometry geometry) throws ObjectStreamException {
        try {
            if (Geometry.isMultiPath(geometry.getType().value())) {
                this.a = new MultiPathData();
            } else if (Geometry.isMultiVertex(geometry.getType().value())) {
                this.a = new MultiVertexData();
            } else {
                this.a = new BaseGeometryData();
            }
            this.a.b = GeometryEngine.geometryToEsriShape(geometry);
            this.a.a = geometry.getType();
            if (Geometry.isMultiVertex(this.a.a.value())) {
                MultiVertexData multiVertexData = (MultiVertexData) this.a;
                MultiVertexGeometryImpl multiVertexGeometryImpl = (MultiVertexGeometryImpl) geometry._getImpl();
                multiVertexData.e = multiVertexGeometryImpl.m_simpleTolerance;
                multiVertexData.d = multiVertexGeometryImpl.getIsSimple(Utils.DOUBLE_EPSILON);
                if (geometry.isEmpty() || !Geometry.isMultiPath(this.a.a.value())) {
                    return;
                }
                MultiPathData multiPathData = (MultiPathData) this.a;
                MultiPathImpl multiPathImpl = (MultiPathImpl) geometry._getImpl();
                multiPathData.c = new boolean[multiPathImpl.getPathCount()];
                AttributeStreamOfInt8 pathFlagsStreamRef = multiPathImpl.getPathFlagsStreamRef();
                int pathCount = multiPathImpl.getPathCount();
                for (int i = 0; i < pathCount; i++) {
                    multiPathData.c[i] = (pathFlagsStreamRef.read(i) & 4) != 0;
                }
            }
        } catch (Exception unused) {
            throw new InvalidObjectException("Cannot serialize this geometry");
        }
    }
}
